package com.aa.activity;


import java.util.ArrayList;
import java.util.List;
import com.aa.CallWaitActivity;
import com.aa.Common;
import com.aa.ContactsActivity;
import com.aa.LatestActivity;
import com.aa.R;
import com.aa.YaloeApplication;
import com.aa.contacts.KaguPhones;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.renderscript.Sampler.Value;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class DialActivity extends Activity implements OnClickListener{

	private  Button btn0 ;
	private  Button btn1 ;
	private  Button btn2 ;
	private  Button btn3 ;
	private  Button btn4 ;
	private  Button btn5 ;
	private  Button btn6 ;
	private  Button btn7 ;
	private  Button btn8;
	private  Button btn9 ;
	private  Button btnStar ;
	private  Button btnHash ;
	private  ImageView mivDel;
	private  Button BtnCall1;
	private  Button btnCall2;
	private  EditText metNumber;
	private Button mDisplayContacts;
	private List<KaguPhones> mContactList;
	private List<KaguPhones> mSearchList = new ArrayList();
	private ListView mListView;
	private View mView;
	private Dialog mDialog;
	private TextView mtvDisFirstName;
	private TextView mtvDisFistNum;
	private LinearLayout mlaySearch;
	private LinearLayout mlayDisFiistLinkman;
	private Button mbtnSearchNum;
	private AAQvodPlay mAaQvodPlay;
	private SearchContactAdapter mAdapter;
	private static final String[] PHONES_PROJECTION = { "_id", "display_name", "data1", "sort_key", "photo_id", "contact_id" };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dial);
		mAaQvodPlay = AAQvodPlay.getInstance();
		mContactList = YaloeApplication.getInstance().contactList;
		initView();
	}
	
	private void initView(){
		mivDel = (ImageView)findViewById(R.id.dial_sd_del);
		mivDel.setOnClickListener(this);
		btn0 = (Button)findViewById(R.id.key_btn0);
		btn0.setOnClickListener(this );
		btn1 = (Button)findViewById(R.id.key_btn1);
		btn1.setOnClickListener(this );
		btn2 = (Button)findViewById(R.id.key_btn2);
		btn2.setOnClickListener(this );
		btn3 = (Button)findViewById(R.id.key_btn3);
		btn3.setOnClickListener(this );
		btn4 = (Button)findViewById(R.id.key_btn4);
		btn4.setOnClickListener(this );
		btn5 = (Button)findViewById(R.id.key_btn5);
		btn5.setOnClickListener(this );
		btn6 = (Button)findViewById(R.id.key_btn6);
		btn6.setOnClickListener(this );
		btn7 = (Button)findViewById(R.id.key_btn7);
		btn7.setOnClickListener(this );
		btn8 = (Button)findViewById(R.id.key_btn8);
		btn8.setOnClickListener(this );
		btn9 = (Button)findViewById(R.id.key_btn9);
		btn9.setOnClickListener(this );
		btnStar = (Button)findViewById(R.id.key_btn_star);
		btnStar.setOnClickListener(this );
		btnHash = (Button)findViewById(R.id.key_btn_hash);
		btnHash.setOnClickListener(this );
		BtnCall1 = (Button)findViewById(R.id.key_dial_call1);
		BtnCall1.setOnClickListener(this );
		btnCall2 = (Button)findViewById(R.id.key_dial_call2);
		btnCall2.setOnClickListener(this );
		metNumber = (EditText)findViewById(R.id.dial_num_text);
		mtvDisFirstName = (TextView)findViewById(R.id.tv_search_name);
		mtvDisFistNum = (TextView)findViewById(R.id.tv_search_num);
		mlaySearch = (LinearLayout)findViewById(R.id.lay_search_display);
		mbtnSearchNum = (Button)findViewById(R.id.btn_search_num);
		mbtnSearchNum.setOnClickListener(this);
		mlayDisFiistLinkman = (LinearLayout)findViewById(R.id.lay_fist_linkman);
		mlayDisFiistLinkman.setOnClickListener(this);
		
		mView = LayoutInflater.from(DialActivity.this).inflate(R.layout.dial_search_result, null);
		mListView = (ListView)mView.findViewById(R.id.lv_search_linkman);
		mListView.setOnItemClickListener(new OnSearchContactClickListener());
		metNumber.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				mSearchList.clear();
				if (s.length() > 2) {
					for (int i = 0; i < mContactList.size(); i++) {
						KaguPhones _KaguPhones = (KaguPhones)mContactList.get(i);
						if (_KaguPhones.getPhoneNum().contains(s)) {
							mSearchList.add(_KaguPhones);
						}
					}					
					
				}
				if (mSearchList.size() > 0){
					mlaySearch.setVisibility(View.VISIBLE);
					int size = mSearchList.size();
					
					
					mbtnSearchNum.setText(String.valueOf(size));
					KaguPhones _KaguPhones = (KaguPhones)mSearchList.get(0);
					mtvDisFirstName.setText(_KaguPhones.getDisplayName());
					mtvDisFistNum.setText(_KaguPhones.getPhoneNum());
				}
				
				if (mSearchList.size() <= 0 || s.length() <= 2) {
					mtvDisFirstName.setText("");
					mtvDisFistNum.setText("");
					mlaySearch.setVisibility(View.GONE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {								
			}
			
			@Override
			public void afterTextChanged(Editable s) {				
				
			}
		});
	}
	
	 private void setSearchAdapter(){
		 if (mSearchList.size() == 0 ) {
			return;
		}
		
		
		 mAdapter = new SearchContactAdapter(mSearchList);
		 mListView.setAdapter(mAdapter);
		 if (mDialog != null) {
				mDialog.show();
				return;
			}
		 mDialog = new AlertDialog.Builder(mAaQvodPlay)
		 .setTitle(R.string.app_search_result)
		 		 .setView(mView)
		 		 
		 		 .setNegativeButton(R.string.app_cancel, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();					
					}
				} ).create();
				
		 mDialog.show();
		 
		 
	 }
	 
	 private class OnSearchContactClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mDialog.cancel();
			mlaySearch.setVisibility(View.GONE);
			//AdapterContextMenuInfo _AdapterContextMenuInfo = (AdapterContextMenuInfo)arg0.getAdapter();
			//int position = _AdapterContextMenuInfo.position;
			KaguPhones _KaguPhones = (KaguPhones)arg0.getAdapter().getItem(arg2);
			metNumber.setText(_KaguPhones.getPhoneNum());
								
		}

		
		 
	 }
	
	 private void keyPressed(int pInt){
	   
	    KeyEvent KeyEvent = new KeyEvent(0, pInt);
	    boolean bool = metNumber.onKeyDown(pInt, KeyEvent);
	  
	    int i = Common.iCallNumber.length();
	    if (i > 0)
	      Common.iCallNumber = Common.iCallNumber.substring(0, i - 1);
	  }
	 
	 public void submitCall(){		
		Common.InitCallSetData(this, 0);
//		if (Common.isSystemPhoneCall(Common.iCallNumber))
//		{
//		    Intent _Intent = new Intent();
//		    _Intent.setAction("android.intent.action.CALL");
//		    _Intent.setData(Uri.parse("tel:" + Common.iCallNumber));
//		    startActivity(_Intent);
//		    return;
//		 }
//		
		Intent _Intent = new Intent();
		_Intent.setClass(this, CallWaitActivity.class);
		startActivity(_Intent);
	 }
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_btn0:
			keyPressed(7);
			break;
		case R.id.key_btn1:
			keyPressed(8);
			break;
		case R.id.key_btn2:
			keyPressed(9);
			break;
		case R.id.key_btn3:
			keyPressed(10);
			break;
		case R.id.key_btn4:
			keyPressed(11);
			break;
		case R.id.key_btn5:
			keyPressed(12);
			break;
		case R.id.key_btn6:
			keyPressed(13);
			break;
		case R.id.key_btn7:
			keyPressed(14);
			break;
		case R.id.key_btn8:
			keyPressed(15);
			break;
		case R.id.key_btn9:
			keyPressed(16);
			break;
		case R.id.key_btn_star:
			keyPressed(17);
			break;
		case R.id.key_btn_hash:
			keyPressed(18);
			break;
		case R.id.key_dial_call1:
			if (metNumber.getText() == null || metNumber.getText().toString().equals("")) {
				Toast.makeText(this, R.string.app_msg_phone_number_1, 3000).show();
				return;
			}
			String _Number = metNumber.getText().toString();
			if (_Number != null) {
				Common.iCallNumber = _Number;
			}
			submitCall();
			metNumber.setText("");
			break;
		case R.id.key_dial_call2:
			
		case R.id.dial_sd_del:
			keyPressed(67);
			break;
		case R.id.btn_search_num:
			
			setSearchAdapter();
			break;
		case R.id.lay_fist_linkman:
			if (mSearchList.size() != 0) {
				metNumber.setText(((KaguPhones)mSearchList.get(0)).getPhoneNum());
			}
			break;
		}		
		
	}
	
	private class Holder{
		TextView tvName;
		TextView tvNumber;
	}
	
	private class SearchContactAdapter extends BaseAdapter{

		private List<KaguPhones> list;
		private LayoutInflater inflater;
		
		public SearchContactAdapter(List<KaguPhones> pList){
			list = pList;
			inflater = LayoutInflater.from(DialActivity.this);
		}
		@Override
		public int getCount() {
			
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder _Holder;
			
			if (convertView == null) {
				_Holder = new Holder();
				convertView = LayoutInflater.from(DialActivity.this).inflate(R.layout.contact_item, null);
				_Holder.tvName = (TextView)convertView.findViewById(R.id.tv_name);
				_Holder.tvNumber = (TextView)convertView.findViewById(R.id.tv_number);
				convertView.setTag(_Holder);
			}else {
				_Holder = (Holder)convertView.getTag();
			}
			KaguPhones _KaguPhones = (KaguPhones)list.get(position);
			
			_Holder.tvName.setText(_KaguPhones.getDisplayName());
			_Holder.tvNumber.setText(_KaguPhones.getPhoneNum());
			return convertView;
			
		}
		
	}

}
